package org.scboy.rbac.service.imp;


import java.util.Date;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacOrgResourceDao;
import org.scboy.rbac.service.OrgResourceService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.lang.UUID;

/**
 * 
 * 组织角色业务类
 * @author yanghao
 *
 */
 @Service
public class OrgResourceServiceImp implements OrgResourceService {

		@Autowired
		EntityFactory factory;
		
	 /**
	   * 分页查询组织关联角色
	   * @param model 查询参数
	   * @return
	   */			 
	 
	@Override
	public Page page(Model model) {
		RbacOrgResourceDao dao=factory.getDao(RbacOrgResourceDao.class);
		return dao.queryPage(model);
	}
    
	 /**
	   * 新增组织角色
	   * @param orgId 组织ID
	   * @param resId 资源ID
	   * @return
	   */		
	@Override
	public boolean add(String orgId, String resId) {
		RbacOrgResourceDao dao=factory.getDao(RbacOrgResourceDao.class);
		Long count=dao.count(Model.create()
				.add("orgId", orgId)
				.add("resId", resId));
		if(count>0){
			return false;
		}
		Model model=Model.create()
				.add("id", UUID.randomUUID().toString())
				.add("orgId", orgId)
				.add("resId", resId)
				.add("addTime", new Date())
				.add("editTime", new Date());
		int result=dao.insert(model);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	 /**
	   * 删除组织角色
	   * @param id 关系ID
	   * @return
	   */	
	@Override
	public boolean delete(String id) {
		RbacOrgResourceDao dao=factory.getDao(RbacOrgResourceDao.class);
		int result=dao.delete(Model.create().add("id", id));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
    
	 /**
	   * 根据组织ID删除组织角色
	   * @param orgId 组织ID
	   * @return
	   */	
	@Override
	public boolean deleteByOrgId(String orgId) {
		RbacOrgResourceDao dao=factory.getDao(RbacOrgResourceDao.class);
		int result=dao.delete(Model.create().add("orgId", orgId));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	 /**
	   * 根据角色ID删除组织角色
	   * @param roleId 资源ID
	   * @return
	   */
	@Override
	public boolean deleteByResId(String resId) {
		RbacOrgResourceDao dao=factory.getDao(RbacOrgResourceDao.class);
		int result=dao.delete(Model.create().add("resId", resId));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

}
